package mate.dart.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mate.dart.DartBoardSubject;
import mate.dart.ThrowEvent;
import mate.dart.utils.Utils;

public class DartBoardPanel extends JPanel implements ActionListener,
		KeyListener {
	
	private static final long serialVersionUID = 1L;
	private JButton[] buttons = new JButton[63];
	private JTextField textField = new JTextField();
	private DartBoardSubject boardSubject;
	private JCheckBox allDartsCheckBox = new JCheckBox("all darts", true);
	
	public DartBoardPanel(DartBoardSubject subject) {
		this();
		this.boardSubject = subject;
	}
	
	public DartBoardPanel() {
		JPanel buttonsPannel = new JPanel(new GridLayout(21, 3));

		JButton b = null;

		b = createNewButton("25x1", 60);
		buttonsPannel.add(b);
		b = createNewButton("25x2", 61);
		buttonsPannel.add(b);
		b = createNewButton("miss", 62);
		b.setBackground(Color.red);
		buttonsPannel.add(b);

		for (int i = 19; i >= 0; i--) {
			for (int j = 1; j <= 3; j++) {
				b = createNewButton(((i % 20) + 1) + "x"  + j, i);
				buttons[i] = b;
				buttonsPannel.add(b);
			}
		}

		setLayout(new BorderLayout());
		add(buttonsPannel, BorderLayout.CENTER);

		Box box = Box.createHorizontalBox();
		box.add(allDartsCheckBox);
		box.add(textField);
		textField.setFont(Utils.getNormalFont());
		textField.addActionListener(this);
		textField.addKeyListener(this);
		add(box, BorderLayout.NORTH);
	}

	public void actionPerformed(ActionEvent e) {
		if ("miss".equals(e.getActionCommand())) {
			ThrowEvent event = new ThrowEvent("0");
			boardSubject.notifyListeners(event);
		} else if (e.getSource() instanceof JButton) {
			String command = e.getActionCommand();
			ThrowEvent event = new ThrowEvent(command);
			boardSubject.notifyListeners(event);
		} else if (e.getSource() instanceof JTextField) {
			String text = textField.getText();
			if ("".equals(text)) {
				text = "0";
			}
			ThrowEvent event = new ThrowEvent(text, allDartsCheckBox.isSelected());
			boardSubject.notifyListeners(event);
			textField.setText("");
			allDartsCheckBox.setSelected(true);
		}
		textField.requestFocus();
	}

	private JButton createNewButton(String name, int index) {
		JButton b = new JButton(name);
		b.addActionListener(this);
		buttons[index] = b;
		return b;
	}

	public void setBoardSubject(DartBoardSubject boardSubject) {
		this.boardSubject = boardSubject;
	}

	public void keyPressed(KeyEvent e) {
		String suffix = "x1";
		if (e.isAltDown()) {
			suffix = "x2";
		} else if (e.isControlDown()) {
			suffix = "x3";
		}
		
		String eventText = null;
		switch (e.getKeyCode()) { 
			case 91 :eventText = "25" + suffix; break;
			case 80 :eventText = "20" + suffix; break;
			case 79 :eventText = "19" + suffix; break;
			case 73 :eventText = "18" + suffix; break;
			case 85 :eventText = "17" + suffix; break;
			case 89 :eventText = "16" + suffix; break;
			case 84 :eventText = "15" + suffix; break;
			case 82 :eventText = "14" + suffix; break;
			case 69 :eventText = "13" + suffix; break;
			case 87 :eventText = "12" + suffix; break;
			case 81 :eventText = "11" + suffix; break;
			case 59 :eventText = "10" + suffix; break;
			case 76 :eventText = "9" + suffix; break;
			case 75 :eventText = "8" + suffix; break;
			case 74 :eventText = "7" + suffix; break;
			case 72 :eventText = "6" + suffix; break;
			case 71 :eventText = "5" + suffix; break;
			case 70 :eventText = "4" + suffix; break;
			case 68 :eventText = "3" + suffix; break;
			case 83 :eventText = "2" + suffix; break;
			case 65 :eventText = "1" + suffix; break;
			case 77 : eventText ="0";
		}

		if (eventText == null || "25x3".equals(eventText)) {
			return;
		}
		boardSubject.notifyListeners(new ThrowEvent(eventText));
	}

	public void keyReleased(KeyEvent e) {
		try {
			Integer.parseInt(textField.getText());
		} catch (NumberFormatException nfe) {
			textField.setText("");
		}		
	}

	public void keyTyped(KeyEvent e) {
	}
}
